package xyz.xiaomeiqiu.study.config;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DataSourceContextHolder {

    public static List<Object> dataSourceKeys = new ArrayList<>();

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return DataSourceConfiguration.MASTER_DATASOURCE_KEY;
        }
    };

    public static void setDataSourceKey(String key) {
        if (StringUtils.hasLength(key)) {
            contextHolder.set(key);
        }
    }

    public static String getDataSourceKey() {
        return contextHolder.get();
    }

    public static void clearDataSourceKey() {
        contextHolder.remove();
    }

    public static boolean containDataSourceKey(String key) {
        return dataSourceKeys.contains(key);
    }


    public static boolean addDataSourceKeys(Collection<? extends Object> keys) {
        return dataSourceKeys.addAll(keys);
    }
}
